package com.elite.commons.redis;

import java.util.concurrent.TimeUnit;

import cn.com.callback.apiCreateContractold;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class EliteRedisTemplate extends RedisTemplate<String, Object> implements IRedisTemplate {
    private static final Log log = LogFactory.getLog(EliteRedisTemplate.class);
    private String scope;

    public String getScope() {
        return this.scope;
    }

    public EliteRedisTemplate(String scope) {
        this.scope = scope;
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        this.setKeySerializer(redisSerializer);
        this.setHashKeySerializer(redisSerializer);
    }

    public void set(String key, Object value) {
        log.debug("EliteRedisTemplate set no ttl Key:" + this.scope + key + " Value:" + value.toString());
        this.opsForValue().set(this.scope + key, value);
    }

    public void set(String key, Object value, long ttl, TimeUnit timeUnit) {
        log.debug("EliteRedisTemplate set ttl Key:" + this.scope + key + " Value:" + value.toString());
        // this.opsForValue().set(this.scope + key, value, 10080, timeUnit);
        this.opsForValue().set(this.scope + key, value);
    }

    public Object get(String key) {
        return this.opsForValue().get(this.scope + key);
    }

    public Boolean expire(String key, long timeout, TimeUnit unit) {
        return super.expire(this.scope + key, 10080, unit);
    }

    public Boolean delete(String key) {
        return super.delete(this.scope + key);
    }

    public void convertAndSend(String channel, Object obj) {
        super.convertAndSend(channel, obj);
    }

    public RedisConnectionFactory getConnectionFactory() {
        return super.getConnectionFactory();
    }
}
